﻿#define _CRT_SECURE_NO_WARNINGS
#include "tan.h"
void CreateFood(Snake* ps)
{
	int y = 0;
	int x = 0;
	int i = 0;
	// 使用当前时间作为种子
	srand(time(NULL));
	do 
	{
		i = 0;
		y = rand() % 23 + 2;
		do
		{
			// 生成随机数
			x = rand() % 55 + 1;
		} while (x % 2 != 0);
		
		//不能与蛇身冲突
		Body B = ps->_pSnake;
		while (B)
		{
			if (B->x == x && B->y == y)
			{
				i = 1;
				break;
			}
			B = B->next;
		}
	}while (i);
	Body F = (Body)malloc(sizeof(body));
	ps->_pFood = F;
	ps->_pFood->next = NULL;
	ps->_pFood->x = x;
	ps->_pFood->y = y;
	SetPos(x, y);
	wprintf(L"%lc", FOOD);
}

void CreateBody(Snake** ps)
{
	for (int i = 0; i < Length; i++)
	{
		Body P = (Body)malloc(sizeof(body));
		P->x = X + i * 2;
		P->y = Y;
		P->next = NULL;
		if ((*ps)->_pSnake == NULL)
		{
			(*ps)->_pSnake = P;
		}
		else {
			P->next = (*ps)->_pSnake;
			(*ps)->_pSnake = P;
		}
	}
	Body T = (*ps)->_pSnake;
	while (T)
	{
			SetPos(T->x, T->y);
			wprintf(L"%lc", BODY);
			T = T->next;
	}

	(*ps)->_dir = RIGHT;
	(*ps)->_sleep_time = 200;
	(*ps)->_status = OK;
	(*ps)->_food_weight = 10;
}

void SetPos(short x, short y)
{
	COORD pos = { x, y };
	HACCEL hehe = NULL;
	hehe = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hehe, pos);
}

void WelcomeToGame()
{
	SetPos(40, 14);
	printf("欢迎来到贪吃蛇小游戏\n");
	SetPos(42, 20);
	system("pause");
	system("cls");
	SetPos(25, 14);
	printf("用 ↑. ↓ . ← . → 来控制蛇的移动，按F3加速，F4减速\n");
	SetPos(42, 20);
	system("pause");
	system("cls");
	SetPos(39, 15);
	printf("加速能够得到更高的分数\n");
	SetPos(42, 20);
	system("pause");
	system("cls");
}

void CreateMap()
{
	//上
	int i = 0;
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", L'□');
	}

	//下
	SetPos(0, 26);
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", L'□');
	}
	//左
	for (i = 1; i <= 25; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", L'□');
	}

	//右
	for (i = 1; i <= 25; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", L'□');
	}
}

void SnakeMove(Snake* ps)
{
	//创建一个结点，表示蛇即将到的下一个节点
	Body P = (Body)malloc(sizeof(body));
	switch (ps->_dir)
	{
	case UP:
		P->x = ps->_pSnake->x;
		P->y = ps->_pSnake->y - 1;
		break;
	case DOWN:
		P->x = ps->_pSnake->x;
	    P->y = ps->_pSnake->y + 1;
		break;
	case LEFT:
		P->x = ps->_pSnake->x - 2;
	    P->y = ps->_pSnake->y;
		break;
	case RIGHT:
		P->x = ps->_pSnake->x + 2;
		P->y = ps->_pSnake->y;
		break;
	}
	P->next = ps->_pSnake;
	ps->_pSnake = P;
	SetPos(P->x, P->y);
	wprintf(L"%lc", BODY);

	//判断下一个节点是不是食物
	Body T = ps->_pSnake;
	while (T->next->next)
	{
		T = T->next;
	}
	if (P->x == ps->_pFood->x && P->y == ps->_pFood->y)
	{	
		ps->_score += ps->_food_weight;
		CreateFood(ps);
	}
	else {
		SetPos(T->next->x, T->next->y);
		printf("  ");
		free(T->next);
		T->next = NULL;
	}
}

void Pause()
{
	while (1)
	{
		Sleep(200);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

void PrintHelpInfo()
{
	//打印提⽰信息
	SetPos(64, 15);
	printf("不能穿墙，不能咬到自己.\n");
	SetPos(64, 16);
	printf("用↑.↓.←.→.分别控制蛇的移动\n");
	SetPos(64, 17);
	wprintf(L"F3 为加速，F4 为减速\n");
	SetPos(64, 18);
	wprintf(L"ESC;退出游戏. space;暂停游戏.\n");
	SetPos(64, 20);
}

void GameOver(pSnake ps)
{
	if (ps->_pSnake->x == 0 || ps->_pSnake->x == 56 || ps->_pSnake->y == 0 || ps->_pSnake->y == 27)
		ps->_status = KILL_BY_WALL;
	Body B = ps->_pSnake->next;
	while (B)
	{
		if (B->x == ps->_pSnake->x && B->y == ps->_pSnake->y)
		{
			ps->_status = KILL_BY_SELF;
			break;
		}
		B = B->next;
	}
}

void GameRun(pSnake ps)
{
	PrintHelpInfo();
	do
	{
		SetPos(64, 10);
		printf("总分数:%d\n", ps->_score);
		SetPos(64, 11);
		printf("当前食物的分数:%2d\n", ps->_food_weight);

		if (KEY_PRESS(VK_UP) && ps->_dir != DOWN)
		{
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != UP)
		{
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
		{
			ps->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT)
		{
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			Pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			//正常退出游戏
			ps->_status = END_NORMAL;
		}
		else if (KEY_PRESS(VK_F3))
		{
			//加速
			if (ps->_sleep_time > 80)
			{
				ps->_sleep_time -= 30;
				ps->_food_weight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			//减速
			if (ps->_food_weight > 2)
			{
				ps->_sleep_time += 30;
				ps->_food_weight -= 2;
			}
		}
		SnakeMove(ps);//蛇走一步的过程
		Sleep(ps->_sleep_time);
		GameOver(ps);
	} while (ps->_status == OK);
	SetPos(25, 13);
	printf("游戏结束!");
}

	void GameStart(Snake * ps)
	{
		system("mode con cols=100 lines=30");
		system("title 贪吃蛇");
		HANDLE hOutput = NULL;
		//获取标准输出的句柄(⽤来标识不同设备的数值)
		hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

		CONSOLE_CURSOR_INFO CursorInfo;
		GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
		CursorInfo.bVisible = false; // 设置光标不可见
		SetConsoleCursorInfo(hOutput, &CursorInfo);
		//1. 打印环境界面和功能介绍
		WelcomeToGame();
		//2. 绘制地图
		CreateMap();
		//3. 创建蛇
		CreateBody(&ps);
		//4. 创建食物
		CreateFood(ps);
		//5. 蛇的移动
		//SnakeMove(ps);
		//6. 暂停
		//Pause(ps);
		//7. 游戏运行
		GameRun(ps);
		//8. 游戏结束条件
	}